<?php
/**
 * Created by PhpStorm.
 * User: W
 * Date: 2019/1/2
 * Time: 8:59
 */

namespace app\sports_admin\controller;
use app\sports_admin\model\Notic as NoticModel;
use app\sports_admin\model\NoticCat;
use think\Request;
use app\sports_admin\model\School;
use app\exception\NoticException;

class Notic extends Base
{
    protected $beforeActionList = [
        'isData'=>['only'=>'index,noticlist,noticdel,noticupdatepage,noticupdate,noticaddpage,noticadd'],
        'operationSelect'=>['only'=>'index,noticlist,noticupdatepage,noticaddpage'],
        'operationInsert'=>['only'=>'noticadd'],
        'operationUpdate'=>['only'=>'noticupdate'],
        'operationDelete'=>['only'=>'noticdel']
    ];
    //教研文件列表页
    public function index()
    {
        $school = School::field("school_id,school_name")->select();
        $data = NoticCat::where("type",1)->select();
        return view("index",compact("data","school"));
    }
    //教研文件列表
    public function noticList()
    {
        $params = Request::instance()->only(['page','rows','sort','sortOrder','searchName','searchType','school']);
        $page = intval($params['page']) ? intval($params['page']) : 1;
        $rows = intval($params['rows']) ? intval($params['rows']) : 15;
        $sort = trim($params['sort']) ? trim($params['sort']) : 'id';
        $sortOrder = trim($params['sortOrder']) ? trim($params['sortOrder']) : 'asc';
        $searchName = trim($params['searchName']);
        $searchType = trim($params['searchType']);
        $school = trim($params['school']);
        $result = NoticModel::noticList($page,$rows,$sort,$sortOrder,$searchName,$searchType,$school);
        return json($result);
    }
    //删除教研文件
    public function noticDel(Request $request)
    {
        if($request->isAjax()){
            $data = NoticModel::noticDel();
            if(!$data){
                throw new NoticException([
                    'msg'=>'删除失败',
                    'errorCode'=>400
                ]);
            }
            return json(['code'=>202,'msg'=>'删除成功']);
        }
    }
    //编辑信息页面
    public function noticUpdatePage()
    {
        $data = NoticModel::message();
        return view('update',compact("data"));
    }
    //编辑信息
    public function noticUpdate(Request $request)
    {
        if($request->isAjax()){
            $data = NoticModel::noticUpdate();
            if(!$data){
                throw new NoticException([
                    'msg'=>'修改失败',
                    'errorCode'=>400
                ]);
            }
            return json(['code'=>202,'msg'=>'修改成功']);
        }
    }
    //添加教研文件页面
    public function noticAddPage()
    {
        $school = School::field("school_id,school_name")->select();
        $data = NoticCat::where('type',1)->select();
        return view('add',compact('data','school'));
    }
    //添加教研文件
    public function noticAdd(Request $request)
    {
        if($request->isAjax()){
            $data = NoticModel::noticAdd();
            if(!$data){
                throw new NoticException([
                    'msg'=>'添加失败',
                    'errorCode'=>400
                ]);
            }
            return json(['code'=>202,'msg'=>'添加成功']);
        }
    }
}